package com.senlingspace.payment.lakala.service.impl;

import com.alibaba.fastjson2.JSON;
import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.exception.SDKException;
import com.lkl.laop.sdk.request.V3LabsTransPreorderRequest;
import com.lkl.laop.sdk.request.model.V3LabsTradeLocationInfo;
import com.lkl.laop.sdk.request.model.V3LabsTradePreorderWechatBus;
import com.senlingspace.common.core.dingtalk.DingTalkService;
import com.senlingspace.common.exception.payment.LakalaException;
import com.senlingspace.payment.config.properties.LakalaProperties;
import com.senlingspace.payment.lakala.bean.request.LakalaPreorderRequest;
import com.senlingspace.payment.lakala.bean.response.LakalaPreorderResponse;
import com.senlingspace.payment.lakala.service.LakalaPreorderService;
import com.senlingspace.system.domain.SlkjMerchant;
import com.senlingspace.system.mapper.SlkjMerchantMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * lakala聚合主扫服务实现类
 *
 * <a href="https://o.lakala.com/#/home/document/detail?id=110">聚合主扫</a>
 *
 * @author lebronjames
 * @date 2024/09/29
 */
@AllArgsConstructor
@Slf4j
@Service
public class LakalaPreorderServiceImpl implements LakalaPreorderService {

    private final LakalaProperties lakalaProperties;
    private final DingTalkService dingTalkService;

    private final SlkjMerchantMapper slkjMerchantMapper;


    @Override
    public LakalaPreorderResponse preorder(LakalaPreorderRequest req) throws LakalaException {
        SlkjMerchant merchant = slkjMerchantMapper.selectByShopId(req.getShopId());
        if (Objects.isNull(merchant) || StringUtils.isBlank(merchant.getTermNo()) || StringUtils.isBlank(merchant.getMerchantNo())) {
            log.error("门店 [{}] 商户信息配置有误！", req.getShopId());
            throw new LakalaException("门店商户信息有误！");
        }

        V3LabsTransPreorderRequest request = getPreorderRequest(req, merchant);

        log.info("Lakala聚合主扫请求参数：{}", JSON.toJSONString(request));
        try {
            String json = LKLSDK.httpPost(request);
            log.info("Lakala聚合主扫响应：{}", json);
            if (StringUtils.isBlank(json)) {
                log.error("拉卡拉聚合主扫异常，返回结果为空!");
                dingTalkService.sendTextMessage("拉卡拉聚合主扫异常，对应入参：" + JSON.toJSONString(request), true);
                return null;
            }
            return JSON.parseObject(json, LakalaPreorderResponse.class);
        } catch (SDKException e) {
            log.error("拉卡拉聚合主扫异常：{}", e.getMessage());
            dingTalkService.sendTextMessage("拉卡拉聚合主扫异常：" + e.getMessage() + "，对应入参：" + JSON.toJSONString(request), true);
            return null;
        }
    }

    private V3LabsTransPreorderRequest getPreorderRequest(LakalaPreorderRequest req, SlkjMerchant merchant) {
        V3LabsTransPreorderRequest request = new V3LabsTransPreorderRequest();
        request.setMerchantNo(merchant.getMerchantNo());
        request.setTermNo(merchant.getTermNo());
        request.setOutTradeNo(req.getPaymentRequestId());
        request.setAccountType("WECHAT");// 固定值
        request.setTransType("71");// 固定值
        request.setTotalAmount(req.getTotalAmount());
        request.setLocationInfo(new V3LabsTradeLocationInfo(lakalaProperties.getServerIpAddress()));
        request.setSubject(req.getSubject());
        request.setRemark(req.getRemark());

        V3LabsTradePreorderWechatBus wechatBus = new V3LabsTradePreorderWechatBus();
        // 微信小程序 appid
        wechatBus.setSubAppid(req.getAppId());
        // 用户的 openid
        wechatBus.setUserId(req.getOpenId());
        request.setAccBusiFields(wechatBus);
        return request;
    }

}
